/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package login.bean;

/**
 *
 * @author Javier
 */
import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import login.business.LoginBusiness;

@ManagedBean
@SessionScoped
public class LoginBean implements Serializable {

    private String conectar;
    private String host;
    private String user;
    private String password;
    private String port;
    public Connection currentConnection;

    public LoginBean() {
    }

    public Connection getCurrentConnection() {
        return currentConnection;
    }

    public void setCurrentConnection(Connection currentConnection) {
        this.currentConnection = currentConnection;
    }

    public String getPort() {
        return port;
    }

    public String getHost() {
        return host;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setConectar(String conectar) {
        this.conectar = conectar;
    }

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConectar() {
        return conectar;
    }

    public void logout() {
        ExternalContext ctx
                = FacesContext.getCurrentInstance().getExternalContext();
        String ctxPath
                = ((ServletContext) ctx.getContext()).getContextPath();
        try {
            ((HttpSession) ctx.getSession(false)).invalidate();
            System.out.println("entra try");
            ctx.redirect(ctxPath + "/faces/index.xhtml");
        } catch (IOException ex) {
            System.out.println("entra catch");
            ex.printStackTrace();
        }
        try {
            currentConnection.close();
            PrincipalPageBean.centerPage = "vacio";
        } catch (SQLException ex) {
            System.out.println("NO EXISTE CONEXIÓN");
        }
    }
    public String begin(){
        FacesContext fc2 = FacesContext.getCurrentInstance();
        if (fc2.getExternalContext().getSessionMap().containsKey("tableBean")) {
            System.out.println("entro a borrar");
            fc2.getExternalContext().getSessionMap().remove("tableBean");
        }
        FacesContext fc = FacesContext.getCurrentInstance();
        if (fc.getExternalContext().getSessionMap().containsKey("advancedPrivilegesBean")) {
            fc.getExternalContext().getSessionMap().remove("advancedPrivilegesBean");
        }
        PrincipalPageBean.centerPage = "vacio";
        return "principalPage.xhtml?faces-redirect=true";
    }

    public String login() {
        FacesContext context = FacesContext.getCurrentInstance();

        LoginBusiness conectionBusiness = new LoginBusiness(user, password, host, port);
        try {
            currentConnection = conectionBusiness.login();
            context.addMessage(null, new FacesMessage("Bienvenido", "Hola " + user.toUpperCase()));
            return "principalPage";
        } catch (SQLException ex) {
            context.addMessage(null, new FacesMessage("Ocurrió un error!!!", ex.getLocalizedMessage()));
            System.out.println(ex.getLocalizedMessage());
            return "";
        }
    }
}
